package indi.flynn.zhouyi.log.example;

import indi.flynn.zhouyi.log.BizFlowLogger;
import indi.flynn.zhouyi.log.annotations.BizFlowLog;
import indi.flynn.zhouyi.log.enums.BizFlowEnum;
import indi.flynn.zhouyi.log.enums.BizFlowNodeEnum;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController()
@RequestMapping("/log")
public class LogController {

    /*
     * 直接使用
     */
    @GetMapping("/msg")
    public String msg(String userName, String email) {
        BizFlowLogger.BizFlowId bizFlowId = BizFlowLogger.createBizFlowId()
                .addParam("userName", userName)
                .addParam("email", email);

        BizFlowLogger.initContext(BizFlowEnum.USER_REGISTER, bizFlowId);

        BizFlowLogger.logNodeSuccess(BizFlowNodeEnum.USER_VALIDATE, "用户信息校验通过");

        BizFlowLogger.BizLogContent bizLogContent = BizFlowLogger.createBizLogContent("error", "数据库操作异常").addParam("key", "value");
        BizFlowLogger.logNodeFail(BizFlowNodeEnum.USER_VALIDATE, "用户信息校验通过（带额外参数）", bizLogContent);

        BizFlowLogger.clearContext();

        return "ok";
    }

    /*
     * 注解使用
     */
    @GetMapping("/msg2")
    @BizFlowLog(
            value = BizFlowEnum.USER_REGISTER,
            bizIdParams = {"userName=#userName", "email=#email"}
    )
    public String msg2(String userName, String email) {

        BizFlowLogger.logNodeSuccess(BizFlowNodeEnum.USER_VALIDATE, "用户信息校验通过(注解方式)");

        return "ok";
    }
}
